﻿
#ifndef BoundingFrustum_h
#define BoundingFrustum_h

#pragma region License
/*
MIT License
Copyright © 2006 The Mono.Xna Team

All rights reserved.

Authors:
Olivier Dufour (Duff)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma endregion License

#include <Typedef.h>
#include <Defines.h>
#include <CSharpPropertyMethods.h>

#include <System/Object.h>
#include <System/Byte.h>
#include <System/Array.h>
#include <System/Nullable.h>
#include <System/Generic/IEquatable.h>
#include <System/Collections/Generic/IEnumerable.h>
#include <System/ArgumentOutOfRangeException.h>
#include <System/ArgumentNullException.h>
#include <System/ArgumentException.h>
#include <System/Text/StringBuilder.h>

#include <Riccsson/Xna/Framework/Vector3.h>
#include <Riccsson/Xna/Framework/RMatrix.h>
#include <Riccsson/Xna/Framework/ContainmentType.h>
#include <Riccsson/Xna/Framework/PlaneIntersectionType.h>
#include <Riccsson/Xna/Framework/Plane.h>

namespace Riccsson
{
	namespace Xna
	{
		namespace Framework
		{
			// Forward declerations.
			struct BoundingBox;
			struct BoundingSphere;
			struct Ray;

			struct BoundingFrustum
				: public Riccsson::System::Generic::IEquatable<BoundingFrustum>
                , public Riccsson::System::object
			{
				#pragma region private: Fields

                PROP3(BoundingFrustum, Matrix, matrix)
				PROP3GET(BoundingFrustum, Plane, Near)
				PROP3GET(BoundingFrustum, Plane, Far)
				PROP3GET(BoundingFrustum, Plane, Left)
				PROP3GET(BoundingFrustum, Plane, Right)
				PROP3GET(BoundingFrustum, Plane, Top)
				PROP3GET(BoundingFrustum, Plane, Bottom)

				private: Matrix _matrix;
				private: GC_PTR<Riccsson::System::Array<Vector3>> corners;
				private: GC_PTR<Riccsson::System::Array<Plane>> planes;

				private: static const int PlaneCount = 6;

				#pragma endregion private: Fields

				#pragma region public: Fields
				public: static const int CornerCount = 8;
				#pragma endregion

				#pragma region public: Constructors

				public: BoundingFrustum(Matrix value);
						
				COPY_CONSTRUCTOR(BoundingFrustum)
				{
					PROP3_INIT_COPY(BoundingFrustum, Matrix, matrix);
					PROP3GET_INIT_COPY(BoundingFrustum, Plane, Near);
					PROP3GET_INIT_COPY(BoundingFrustum, Plane, Far);
					PROP3GET_INIT_COPY(BoundingFrustum, Plane, Left);
					PROP3GET_INIT_COPY(BoundingFrustum, Plane, Right);
					PROP3GET_INIT_COPY(BoundingFrustum, Plane, Top);
					PROP3GET_INIT_COPY(BoundingFrustum, Plane, Bottom);
				}

				#pragma endregion public: Constructors


				#pragma region public: Properties

				public: PROP3_GET(Matrix, matrix);
				public: PROP3_SET(Matrix, matrix);

				public: PROP3_GET(Plane, Near);
        
				public: PROP3_GET(Plane, Far);
        
				public: PROP3_GET(Plane, Left);

				public: PROP3_GET(Plane, Right);

				public: PROP3_GET(Plane, Top);

				public: PROP3_GET(Plane, Bottom);

				#pragma endregion public: Properties

				#pragma region public: Methods

				public: ContainmentType Contains(BoundingBox& box);

				public: void Contains(BoundingBox ref box, ContainmentType out result);

				// TODO: Implement this
				public: ContainmentType Contains(BoundingFrustum frustum);

				public: ContainmentType Contains(BoundingSphere& sphere);

				public: void Contains(BoundingSphere ref sphere, ContainmentType out result);

				public: ContainmentType Contains(Vector3 point);

				public: void Contains(Vector3 ref point, ContainmentType out result);

				public: bool Equals(BoundingFrustum* other);

				public: override bool Equals(Riccsson::System::object* obj);

				public: GC_PTR<Riccsson::System::Array<Vector3>> GetCorners();
		
				public: void GetCorners(Riccsson::System::Array<Vector3>* corners);

				public: override int GetHashCode();

				public: bool Intersects(BoundingBox& box);

				public: void Intersects(BoundingBox ref box, bool out result);

				public: bool Intersects(BoundingFrustum frustum);

				public: bool Intersects(BoundingSphere& sphere);

				public: void Intersects(BoundingSphere ref sphere, bool out result);

				public: PlaneIntersectionType Intersects(Plane plane);

				public: void Intersects(Plane ref plane, PlaneIntersectionType out result);

				public: Riccsson::System::Nullable<float> Intersects(Ray& ray);

				public: void Intersects(Ray ref ray, Riccsson::System::Nullable<float> out result);

				public: override GC_PTR<Riccsson::System::string> ToString();

				#pragma endregion public: Methods


				#pragma region private: Methods

				private: void CreateCorners();

				private: void CreatePlanes();

				private: static void IntersectionPoint(Plane ref a, Plane ref b, Plane ref c, Vector3 out result);
        
				private: void NormalizePlane(Plane ref p);

				#pragma endregion
			};
            
			inline bool operator ==(BoundingFrustum a, BoundingFrustum b)
			{
				if (Riccsson::System::object::Equals(&a, null))
					return (Riccsson::System::object::Equals(&b, null));

				if (Riccsson::System::object::Equals(&b, null))
					return (Riccsson::System::object::Equals(&a, null));

				return a.matrix == (b.matrix);
			}

			inline bool operator !=(BoundingFrustum a, BoundingFrustum b)
			{
				return !(a == b);
			}
		}
	}
}

#endif
